﻿namespace HiMap.MapControls.Tools
{
    using HiMap.Carto;
    using HiMap.Geometry;
    using HiMap.MapControls;
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Reflection;
    using System.Windows.Forms;

    public class ToolDrawGeometry : ToolClass
    {
        private IFeatureLayer _lyr;
        private DrawControl ctl;

        public ToolDrawGeometry(IFeatureLayer lyr)
        {
            this._lyr = lyr;
        }

        private void MoveDraw(double Scale)
        {
            List<IPoint> mapPoints;
            List<Point> displayPoints;
            IPoint point = base.Map.PointToMapPoint(this.ctl.Location.X, this.ctl.Location.Y);
            this.ctl.DrawStop(true, out mapPoints, out displayPoints);
            base.Map.Map.MapScale = Scale;
            base.Map.MapRefresh();
            this.ctl.Img = base.Map.MapImage;
            for (int i = 0; i < mapPoints.get_Count(); i++)
            {
                this.ctl.CreatePoint(base.Map.MapToScreen(mapPoints.get_Item(i).X, mapPoints.get_Item(i).Y));
            }
            this.ctl.Location = base.Map.MapToScreen(point.X, point.Y);
            this.ctl.Show(base.Map.MapToScreen(point.X, point.Y));
        }

        private void OnBottomLeft_Click(object sender, EventArgs e)
        {
            this.MoveDraw(base.Map.Map.MapScale / 2);
        }

        private void OnBottomRight_Click(object sender, EventArgs e)
        {
            this.MoveDraw(base.Map.Map.MapScale * 2);
        }

        protected override void OnMouseDown(object sender, MouseEventArgs e)
        {
            this.ctl.Location = new Point(e.X - (this.ctl.Width / 2), e.Y - (this.ctl.Height / 2));
            this.ctl.Show(new Point(e.X - (this.ctl.Width / 2), e.Y - (this.ctl.Height / 2)));
            this.ctl.CreatePoint(new Point(e.X, e.Y));
            this.ctl.Visible = true;
        }

        protected override void OnMouseMove(object sender, MouseEventArgs e)
        {
        }

        protected override void OnMouseUp(object sender, MouseEventArgs e)
        {
        }

        protected override void OnToolCreate(MapControl Map)
        {
            base.OnToolCreate(Map);
            this.ctl = new DrawControl();
            this.ctl.EdgeMonitor = true;
            Map.Controls.Add(this.ctl);
            this.ctl.BringToFront();
            this.ctl.Map = base.Map;
            this.ctl.Visible = false;
            Assembly executingAssembly = Assembly.GetExecutingAssembly();
            Bitmap bitmap = new Bitmap(executingAssembly.GetManifestResourceStream("HiMap.MapControls.ResourceFile.save_16x16.bmp"));
            Bitmap bitmap2 = new Bitmap(executingAssembly.GetManifestResourceStream("HiMap.MapControls.ResourceFile.trash_(delete)_16x16.bmp"));
            Bitmap bitmap3 = new Bitmap(executingAssembly.GetManifestResourceStream("HiMap.MapControls.ResourceFile.zoom_in_16x16.bmp"));
            Bitmap bitmap4 = new Bitmap(executingAssembly.GetManifestResourceStream("HiMap.MapControls.ResourceFile.zoom_out_16x16.bmp"));
            this.ctl.SetButton(ButtonLocation.TopLeftButton, true, Image.FromHbitmap(bitmap.GetHbitmap()), Image.FromHbitmap(bitmap.GetHbitmap()));
            this.ctl.SetButton(ButtonLocation.TopRightButton, true, Image.FromHbitmap(bitmap2.GetHbitmap()), Image.FromHbitmap(bitmap2.GetHbitmap()));
            this.ctl.SetButton(ButtonLocation.BottomLeftButton, true, Image.FromHbitmap(bitmap3.GetHbitmap()), Image.FromHbitmap(bitmap3.GetHbitmap()));
            this.ctl.SetButton(ButtonLocation.BottomRightButton, true, Image.FromHbitmap(bitmap4.GetHbitmap()), Image.FromHbitmap(bitmap4.GetHbitmap()));
            this.ctl.TopLeft_Click += new EventHandler(this.OnTopLeft_Click);
            this.ctl.TopRight_Click += new EventHandler(this.OnTopRight_Click);
            this.ctl.BottomLeft_Click += new EventHandler(this.OnBottomLeft_Click);
            this.ctl.BottomRight_Click += new EventHandler(this.OnBottomRight_Click);
            if (this._lyr != null)
            {
                IFeatureClass class2 = (IFeatureClass) this._lyr.Class;
                switch (class2.GeometryType)
                {
                    case GeometryType.NotGeometry:
                        this.ctl.DrawType = PicType.Null;
                        return;

                    case GeometryType.Point:
                        this.ctl.DrawType = PicType.Point;
                        return;

                    case GeometryType.MultiPoint:
                        this.ctl.DrawType = PicType.Point;
                        return;

                    case GeometryType.Polyline:
                        this.ctl.DrawType = PicType.Polyline;
                        return;

                    case GeometryType.Polygon:
                        this.ctl.DrawType = PicType.Polygon;
                        return;

                    case GeometryType.Envelope:
                        this.ctl.DrawType = PicType.Null;
                        return;
                }
            }
            else
            {
                this.ctl.DrawType = PicType.Null;
            }
        }

        protected override void OnToolUpdated(object sender, EventArgs e)
        {
            List<IPoint> mapPoints;
            List<Point> displayPoints;
            this.ctl.DrawStop(false, out mapPoints, out displayPoints);
            this.ctl.ControlDispose();
            base.Map.Controls.Remove(this.ctl);
        }

        private void OnTopLeft_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(Setup.GetInfo("ToolEditMessage2"), Setup.GetInfo("ToolEditMessage1"), MessageBoxButtons.YesNo, MessageBoxIcon.None, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
            {
                List<IPoint> mapPoints;
                List<Point> displayPoints;
                this.ctl.DrawStop(true, out mapPoints, out displayPoints);
                this.SaveData(mapPoints);
                MessageBox.Show(Setup.GetInfo("ToolEditMessage4"));
            }
        }

        private void OnTopRight_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(Setup.GetInfo("ToolEditMessage3"), Setup.GetInfo("ToolEditMessage1"), MessageBoxButtons.YesNo, MessageBoxIcon.None, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
            {
                List<IPoint> mapPoints;
                List<Point> displayPoints;
                this.ctl.DrawStop(false, out mapPoints, out displayPoints);
                MessageBox.Show(Setup.GetInfo("ToolEditMessage5"));
            }
        }

        private void SaveData(List<IPoint> pts)
        {
            if (this._lyr != null)
            {
                IFeatureClass class2 = (IFeatureClass) this._lyr.Class;
                IFeature feature = class2.CreateFeature();
                IGeometry geometry = null;
                switch (class2.GeometryType)
                {
                    case GeometryType.NotGeometry:
                        this.ctl.DrawType = PicType.Null;
                        break;

                    case GeometryType.Point:
                    {
                        IPoint point2 = new PointClass();
                        point2.X = pts.get_Item(0).X;
                        point2.Y = pts.get_Item(0).Y;
                        geometry = point2;
                        break;
                    }
                    case GeometryType.MultiPoint:
                    {
                        IMultiPoint point = new MultiPointClass();
                        IGeometryCollection geometrys = point;
                        for (int i = 0; i < pts.get_Count(); i++)
                        {
                            geometrys.AddGeometry(pts.get_Item(i));
                        }
                        geometry = point;
                        break;
                    }
                    case GeometryType.Polyline:
                    {
                        IPolyline polyline = new PolylineClass();
                        IPointCollection points = polyline;
                        for (int j = 0; j < pts.get_Count(); j++)
                        {
                            points.AddPoint(pts.get_Item(j));
                        }
                        geometry = polyline;
                        break;
                    }
                    case GeometryType.Polygon:
                    {
                        IPolygon polygon = new PolygonClass();
                        IPointCollection points2 = polygon;
                        for (int k = 0; k < pts.get_Count(); k++)
                        {
                            points2.AddPoint(pts.get_Item(k));
                        }
                        geometry = polygon;
                        break;
                    }
                    case GeometryType.Envelope:
                        this.ctl.DrawType = PicType.Null;
                        break;
                }
                feature.Shape = geometry;
                feature.Save();
            }
        }
    }
}

